#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-30 13:39:58
# @Description: 字典特征抽取
##############################################
from sklearn.feature_extraction import DictVectorizer

def dic_demo():
    """
    字典特征抽取
    """
    data = [{'city': '北京', 'temperature': 100},
        {'city': '上海', 'temperature': 60},
        {'city': '深圳', 'temperature': 30}]

    # 实例化转换器, sparse 稀疏矩阵 默认Ture(返回位置和位置的值 0值会过滤掉 节省空间)
    transfer = DictVectorizer(sparse=False)
    # 转换
    data_new = transfer.fit_transform(data)
    print("data_new:\n", data_new)
    print("特征名字:\n", transfer.get_feature_names())

if __name__ == '__main__':
   dic_demo()